package com.atguigu.tingshu.payment.service.impl;

import com.atguigu.tingshu.common.util.AuthContextHolder;
import com.atguigu.tingshu.model.payment.PaymentInfo;
import com.atguigu.tingshu.order.client.OrderFeignClient;
import com.atguigu.tingshu.payment.config.WxPayConstantProperties;
import com.atguigu.tingshu.payment.service.PaymentInfoService;
import com.atguigu.tingshu.payment.service.WxPayService;
import com.atguigu.tingshu.user.client.UserFeignClient;
import com.atguigu.tingshu.vo.user.UserInfoVo;
import com.wechat.pay.java.core.RSAAutoCertificateConfig;
import com.wechat.pay.java.service.payments.jsapi.JsapiServiceExtension;
import com.wechat.pay.java.service.payments.jsapi.model.Amount;
import com.wechat.pay.java.service.payments.jsapi.model.Payer;
import com.wechat.pay.java.service.payments.jsapi.model.PrepayRequest;
import com.wechat.pay.java.service.payments.jsapi.model.PrepayWithRequestPaymentResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
@Slf4j
@SuppressWarnings("all")
public class WxPayServiceImpl implements WxPayService {

	@Autowired
	private PaymentInfoService paymentInfoService;


    @Autowired
    private OrderFeignClient orderFeignClient;

    @Autowired
    private UserFeignClient userFeignClient;


    @Autowired
    private WxPayConstantProperties wxPayConstantProperties;

    @Autowired
    private JsapiServiceExtension jsapiServiceExtension;

    @Autowired
    private RSAAutoCertificateConfig config;

    @Override
    public Map<String, String> createJsapiWxPayForm(String paymentType, String orderNo) {
        try {
            //1.新增本地交易记录
            Long userId = AuthContextHolder.getUserId();
            PaymentInfo paymentInfo = paymentInfoService.savePaymentInfo(paymentType, userId, orderNo);
            //2.调用微信API返回拉起微信支付页面参数
            //3.1 调用微信预交易的请求对象 单位：分
            PrepayRequest request = new PrepayRequest();
            Amount amount = new Amount();
            //预交易订单金额 1分
            amount.setTotal(1);
            request.setAmount(amount);
            request.setAppid(wxPayConstantProperties.getAppid());
            request.setMchid(wxPayConstantProperties.getMerchantId());
            request.setDescription(paymentInfo.getContent());
            //设置微信异步通知商户支付结果：回调地址
            request.setNotifyUrl(wxPayConstantProperties.getNotifyUrl());
            request.setOutTradeNo(orderNo);
            //小程序开发阶段-只有小程序开发者才能进行付款 购买者参数上线有不需要设置
            UserInfoVo userInfoVo = userFeignClient.getUserInfoVoByUserId(userId).getData();
            Payer payer = new Payer();
            payer.setOpenid(userInfoVo.getWxOpenId());
            request.setPayer(payer);
            PrepayWithRequestPaymentResponse paymentResponse = jsapiServiceExtension.prepayWithRequestPayment(request);
            if (paymentResponse != null) {
                Map<String, String> map = new HashMap<>();
                map.put("timeStamp", paymentResponse.getTimeStamp());
                map.put("package", paymentResponse.getPackageVal());
                map.put("paySign", paymentResponse.getPaySign());
                map.put("signType", paymentResponse.getSignType());
                map.put("nonceStr", paymentResponse.getNonceStr());
                return map;
            }
        } catch (Exception e) {
            log.error("[支付]微信预支付创建失败：{}", e);
            throw new RuntimeException(e);
        }
        return null;
    }
}
